<template>
  <a-modal
    :open="open"
    width="640px"
    :title="!rowData?.id ? '新增医师分组' : '修改医师分组'"
    centered
    :ok-button-props="{ disabled: !isCompleted }"
    ok-text="保存"
    @ok="onOk"
    @cancel="onCancel"
  >
    <a-form :model="formState" class="form-style-flex">
      <a-form-item label="分组名称" v-bind="validateInfos.groupName" class="w-1/2">
        <a-input v-model:value="formState.groupName" placeholder="请输入" @input="getPinyin()" />
      </a-form-item>
      <a-form-item label="助记码" v-bind="validateInfos.pyjm" class="w-1/2">
        <a-input v-model:value="formState.pyjm" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="备注" class="w-full">
        <a-textarea v-model:value="formState.memo" :rows="5" placeholder="请输入" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script setup>
import { ref, watch } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'

const emit = defineEmits(['update:open', 'success'])
const props = defineProps({
  open: Boolean,
  rowData: {
    type: Object,
    default: () => ({})
  }
})

const formState = ref({
  groupName: ''
})
const rulesState = ref({
  groupName: [{ required: true, message: '请输入分组名称' }],
  pyjm: [{ required: true, message: '请输入助记码' }]
})
const { isCompleted, resetFields, validateInfos } = useForm(formState, rulesState)
const getPinyin = async () => {
  const { data, code } = await post('/common/util/getPinyin', { name: formState.value.groupName })
  code === 0 && (formState.value.pyjm = data)
}
watch(
  () => props.open,
  () => {
    formState.value = { ...props.rowData }
  }
)
const onCancel = () => {
  emit('update:open', false)
  resetFields()
}
const onOk = async () => {
  const { code, msg } = await post('/operation/opex/doEmpGroup', {
    ...formState.value
  })
  if (code !== 0) return message.error(msg)
  message.success(msg || '保存成功')
  emit('success')
  onCancel()
}
</script>
